package data

import (
	"errors"
	"github.com/360EntSecGroup-Skylar/excelize"
	"path"
	"server/model"
	"strconv"
)

func GetCourseItems(male bool) ([]model.CourseItem, error) {
	file, err := excelize.OpenFile("/home/sftp_root/shared/script_index.xlsx")
	if err != nil {
		return nil, err
	}
	var result []model.CourseItem
	rows, err := file.GetRows("Sheet1")
	for index, row := range rows {
		if len(row) < 4 {
			return nil, errors.New("invalid file")
		}
		if index == 0 {
			continue
		}
		if (row[3] == "m" && male) || (row[3] == "f" && !male) {
			result = append(result, model.CourseItem{Target: row[0], Instruction: row[1]})
		}
	}
	return result, nil
}

func GetTourCount(male bool, target string, instruction string) (int, error) {
	file, err := excelize.OpenFile("/home/sftp_root/shared/script_index.xlsx")
	if err != nil {
		return 0, err
	}
	rows, err := file.GetRows("Sheet1")
	var folder string
	var fileName string
	for index, row := range rows {
		if index == 0 {
			continue
		}
		if len(row) < 6 {
			return 0, errors.New("invalid file")
		}
		if ((row[3] == "m" && male) || (row[3] == "f" && !male)) && (row[0] == target) && (row[1] == instruction) {
			folder = row[2]
			fileName = row[5]
			break
		}
	}
	if folder == "" || fileName == "" {
		return 0, errors.New("invalid folder or file")
	}
	scriptPath := path.Join("/home/sftp_root/shared", folder, fileName)
	file, err = excelize.OpenFile(scriptPath)
	if err != nil {
		return 0, err
	}
	tour := 0
	rows, err = file.GetRows("Sheet1")
	for index, row := range rows {
		if index == 0 {
			continue
		}
		if row[0] == "" {
			continue
		}
		currentTour, err := strconv.Atoi(row[0])
		if err != nil {
			return 0, err
		}
		if currentTour > tour {
			tour = currentTour
		}
	}
	return tour, nil
}

func GetUnitCount(male bool, target string, instruction string, tour int) (int, error) {
	file, err := excelize.OpenFile("/home/sftp_root/shared/script_index.xlsx")
	if err != nil {
		return 0, err
	}
	rows, err := file.GetRows("Sheet1")
	var folder string
	var fileName string
	for index, row := range rows {
		if index == 0 {
			continue
		}
		if len(row) < 6 {
			return 0, errors.New("invalid file")
		}
		if ((row[3] == "m" && male) || (row[3] == "f" && !male)) && (row[0] == target) && (row[1] == instruction) {
			folder = row[2]
			fileName = row[5]
			break
		}
	}
	if folder == "" || fileName == "" {
		return 0, errors.New("invalid folder or file")
	}
	scriptPath := path.Join("/home/sftp_root/shared", folder, fileName)
	file, err = excelize.OpenFile(scriptPath)
	if err != nil {
		return 0, err
	}
	currentTour := 0
	lesson := 0
	rows, err = file.GetRows("Sheet1")
	for index, row := range rows {
		if index == 0 {
			continue
		}
		if row[0] != "" {
			currentTour, err = strconv.Atoi(row[0])
			if err != nil {
				return 0, err
			}
		}
		if tour == currentTour {
			if row[1] == "" {
				continue
			}
			currentLesson, err := strconv.Atoi(row[1])
			if err != nil {
				return 0, err
			}
			if currentLesson > lesson {
				lesson = currentLesson
			}
		}
	}
	return lesson, nil
}

func GetScript(male bool, target string, instruction string, tour int, lesson int) ([]model.ScriptItem, error) {
	file, err := excelize.OpenFile("/home/sftp_root/shared/script_index.xlsx")
	if err != nil {
		return nil, err
	}
	rows, err := file.GetRows("Sheet1")
	var folder string
	var fileName string
	for index, row := range rows {
		if index == 0 {
			continue
		}
		if len(row) < 6 {
			return nil, errors.New("invalid file")
		}
		if ((row[3] == "m" && male) || (row[3] == "f" && !male)) && (row[0] == target) && (row[1] == instruction) {
			folder = row[2]
			fileName = row[5]
			break
		}
	}
	if folder == "" || fileName == "" {
		return nil, errors.New("invalid folder or file")
	}
	scriptPath := path.Join("/home/sftp_root/shared", folder, fileName)
	file, err = excelize.OpenFile(scriptPath)
	if err != nil {
		return nil, err
	}
	currentTour := 0
	currentLesson := 0
	currentActivity := ""
	counter := 0
	var result []model.ScriptItem
	rows, err = file.GetRows("Sheet1")
	for index, row := range rows {
		if index == 0 {
			continue
		}
		if row[0] != "" {
			currentTour, err = strconv.Atoi(row[0])
			if err != nil {
				return nil, err
			}
		}
		if tour == currentTour {
			if row[1] != "" {
				currentLesson, err = strconv.Atoi(row[1])
				if err != nil {
					return nil, err
				}
			}
			if lesson == currentLesson {
				if row[3] != "" {
					if counter != 0 {
						result = append(result, model.ScriptItem{Activity: currentActivity, Count: counter})
					}
					currentActivity = row[3]
				}
				counter, err = strconv.Atoi(row[4])
				if err != nil {
					return nil, err
				}
			}
		}
	}
	result = append(result, model.ScriptItem{Activity: currentActivity, Count: counter})
	return result, nil
}
